//不要给我WA WA 叫
//一定AC
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <set>
#include <map>
#include <cstring>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#include <iomanip>
#include <climits>
#define int long long
#define x first 
#define y second 
using namespace std;

int f(int N, int M) 
{
    string strN = to_string(N);
    int res = LLONG_MAX;
    for (int i = 0; i <= strN.size(); ++i)
    {
        for (int j = 0; j <= 9; ++j) 
        {
            string new_num = strN;
            new_num.insert(i, to_string(j));
    
            int k = stoll(new_num);//字符类型转化为long long 型
            
            if (k > N && k % M == 0) 
            {
                res = min(res, k);
            }
        }
    }
    
    return (res == LLONG_MAX) ? -1 : res;
}
void solve()
{
    int n,m;
    cin >> n >> m;
    cout << f(n,m) << endl;
}
signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr),cout.tie(nullptr);
    int t;
    cin >> t;
    while(t -- )  solve();
    return 0;
}
